/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.tukano.misc;

import it.tukano.collections.ArrayListExt;
import it.tukano.collections.ListExt;

/**
 * A term
 * @author utente4
 */
public class Term {
    public static void main(String[] args) {
        Term t = new Term(20, 2, 1, 3);
        System.out.println(t);
    }
    
    private final int years, months, days, weeks;
    private final String description;

    public Term(int years, int months, int weeks, int days) {
        this.years = years;
        this.months = months;
        this.weeks = weeks;
        this.days = days;
        
        String y = null, m = null, w = null, d = null;
        ListExt<String> tokens = new ArrayListExt<String>();
        if(years > 0) y = years + (years == 1 ? " anno" : " anni");
        if(months > 0) m = months + (months == 1 ? " mese" : " mesi");
        if(weeks > 0) w = weeks + (weeks == 1 ? " settimana" : " settimane");
        if(days > 0) d = days + (days == 1 ? " giorno" : " giorni");
        tokens.addAllNonNull(y, m, w, d);
        StringBuilder buffer = new StringBuilder();
        for(int i = 0; i < tokens.size(); i++) {
            String token = tokens.get(i);
            if(tokens.size() > 1 && i == tokens.size() - 1) {
                buffer.append(" e ").append(token);
            } else if(i == 0) {
                buffer.append(token);
            } else {
                buffer.append(", ").append(token);
            }
        }
        description = buffer.toString();
    }

    public int getWeeks() {
        return weeks;
    }

    public int getMonths() {
        return months;
    }

    public int getDays() {
        return days;
    }
    
    public int getYears() {
        return years;
    }

    @Override
    public String toString() {
        return description;
    }
}
